<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>6-4. 鼠标操作刚体 | 被删的前端游乐场</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="Just playing around">
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.1e2670bf.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.38d016d1.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.e3f029cb.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/84.5f766c29.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>Angular框架解读</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>玩转 Angular</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-2b653b36><span class="stand" data-v-2b653b36></span> <div class="cat" data-v-2b653b36><div class="body" data-v-2b653b36></div> <div class="head" data-v-2b653b36><div class="ear" data-v-2b653b36></div> <div class="ear" data-v-2b653b36></div></div> <div class="face" data-v-2b653b36><div class="nose" data-v-2b653b36></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div></div> <div class="tail-container" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>BOX2D.js 文档与教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/box2d-tutorial/1-0-basic-catalog.html" class="sidebar-link">1. 基本概念</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/2-0-world.html" class="sidebar-link">2. 物理世界</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/3-0-shape-and-body.html" class="sidebar-link">3. 形状 shape - 刚体 body</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/4-0-joint.html" class="sidebar-link">4. 关节 joint</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html" class="sidebar-link">5. 操作 operation</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-1-create-and-init-world.html" class="sidebar-link">6-1. 创建世界并初始化</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-2-add-body.html" class="sidebar-link">6-2. 添加刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-3-add-bound.html" class="sidebar-link">6-3. 添加边界</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html" aria-current="page" class="active sidebar-link">6-4. 鼠标操作刚体</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html#获取所在位置刚体" class="sidebar-link">获取所在位置刚体</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html#添加鼠标点击事件" class="sidebar-link">添加鼠标点击事件</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html#操作刚体" class="sidebar-link">操作刚体</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html#完整代码" class="sidebar-link">完整代码</a></li></ul></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-5-handle-contact.html" class="sidebar-link">6-5. 处理碰撞刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/7-0-api.html" class="sidebar-link">7. api</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>本节我们主要讲述用鼠标给刚体添加速度向量的过程。</p> <h1 id="鼠标操作刚体"><a href="#鼠标操作刚体" class="header-anchor">#</a> 鼠标操作刚体</h1> <h2 id="获取所在位置刚体"><a href="#获取所在位置刚体" class="header-anchor">#</a> 获取所在位置刚体</h2> <p>首先，我们需要通过一个位置坐标(x, y)来获取该坐标下的刚体，若无则返回 null。</p> <p>前面我们也提到过，我们可以通过画一个较小的矩形，然后判断是否有相交来获取刚体：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">GetBodyAtPosition</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 首先创建一个近似于点的小区域</span>
  <span class="token keyword">var</span> mousePVec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 利用b2Vec2定义一个矢量，用来保存鼠标点击的点</span>
  <span class="token keyword">var</span> aabb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 利用b2AABB创建一个环境</span>
  aabb<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  aabb<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 设置aabb的左上角及右下角坐标，这里是以鼠标点击位置为中心创建了一个长、宽均为0.002的矩形区域</span>

  <span class="token comment">// 然后查询与指定区域有重叠的刚体</span>
  <span class="token keyword">var</span> k_maxCount <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token comment">// 设定所要查找形状的数量，注意合理设置其大小，过大会影响运行速度</span>
  <span class="token keyword">var</span> shapes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 保存查找到的与已知边界盒相交的形状</span>
  <span class="token keyword">var</span> count <span class="token operator">=</span> world<span class="token punctuation">.</span><span class="token function">Query</span><span class="token punctuation">(</span>aabb<span class="token punctuation">,</span> shapes<span class="token punctuation">,</span> k_maxCount<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 在世界中查找与边界盒相交的maxCount个形状，并返回边界盒区域内实际包含的形状的个数</span>

  <span class="token keyword">var</span> findBody <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 首先设定没有找到物体</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> count<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">IsStatic</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 条件假定查找到的形状不是静态刚体，比如墙</span>
      <span class="token keyword">var</span> tShape <span class="token operator">=</span> shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 将查找到的形状赋给tShape变量</span>
      <span class="token keyword">var</span> inside <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 将tShape对应的刚体赋给inside</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>inside<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 如果inside这个刚体存在</span>
        <span class="token comment">// 那么返回这个刚体，并跳出遍历</span>
        findBody <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> findBody<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="添加鼠标点击事件"><a href="#添加鼠标点击事件" class="header-anchor">#</a> 添加鼠标点击事件</h2> <p>这里我们使用鼠标点击(mousedown)事件，去触发刚体的状态改变。</p> <ul><li>添加事件监听</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code>document<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">&quot;mousedown&quot;</span><span class="token punctuation">,</span> handleMousedown<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>获取鼠标位置</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 获取鼠标坐标</span>
<span class="token keyword">function</span> <span class="token function">getMousePos</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> e <span class="token operator">=</span> event <span class="token operator">||</span> window<span class="token punctuation">.</span>event<span class="token punctuation">;</span>
  <span class="token keyword">var</span> scrollX <span class="token operator">=</span> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span>scrollLeft <span class="token operator">||</span> document<span class="token punctuation">.</span>body<span class="token punctuation">.</span>scrollLeft<span class="token punctuation">;</span>
  <span class="token keyword">var</span> scrollY <span class="token operator">=</span> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span>scrollTop <span class="token operator">||</span> document<span class="token punctuation">.</span>body<span class="token punctuation">.</span>scrollTop<span class="token punctuation">;</span>
  <span class="token keyword">var</span> x <span class="token operator">=</span> e<span class="token punctuation">.</span>pageX <span class="token operator">||</span> e<span class="token punctuation">.</span>clientX <span class="token operator">+</span> scrollX<span class="token punctuation">;</span>
  <span class="token keyword">var</span> y <span class="token operator">=</span> e<span class="token punctuation">.</span>pageY <span class="token operator">||</span> e<span class="token punctuation">.</span>clientY <span class="token operator">+</span> scrollY<span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span> x<span class="token operator">:</span> x<span class="token punctuation">,</span> y<span class="token operator">:</span> y <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>处理鼠标移动</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 处理鼠标移动</span>
<span class="token keyword">function</span> <span class="token function">handleMousedown</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> e <span class="token operator">=</span> e <span class="token operator">||</span> window<span class="token punctuation">.</span>event<span class="token punctuation">;</span>
  <span class="token comment">// 获取鼠标x坐标</span>
  <span class="token keyword">var</span> newMouse <span class="token operator">=</span> <span class="token function">getMousePos</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> selectBody <span class="token operator">=</span> <span class="token function">GetBodyAtPosition</span><span class="token punctuation">(</span>
    newMouse<span class="token punctuation">.</span>x <span class="token operator">-</span> canvas<span class="token punctuation">.</span>offsetLeft<span class="token punctuation">,</span>
    newMouse<span class="token punctuation">.</span>y <span class="token operator">-</span> canvas<span class="token punctuation">.</span>offsetTop
  <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 选择刚体</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>selectBody<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 处理选中的刚体</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">// 未选中时处理</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="操作刚体"><a href="#操作刚体" class="header-anchor">#</a> 操作刚体</h2> <ul><li>给选中刚体添加速度向量</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 若有选中刚体，则处理</span>
<span class="token keyword">var</span> LinearVelocity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 定义一个向量</span>
selectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 激活休眠状态</span>
selectBody<span class="token punctuation">.</span><span class="token function">SetLinearVelocity</span><span class="token punctuation">(</span>LinearVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//给定一个速度向量</span>
</code></pre></div><ul><li>未选中刚体，则新建添加一个矩形刚体</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 若无，则随机生成一个矩形添加</span>
<span class="token keyword">var</span> width <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> height <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> newMouse<span class="token punctuation">.</span>x<span class="token punctuation">,</span> newMouse<span class="token punctuation">.</span>y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="完整代码"><a href="#完整代码" class="header-anchor">#</a> 完整代码</h2> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> canvas<span class="token punctuation">;</span>
<span class="token keyword">var</span> ctx<span class="token punctuation">;</span>
<span class="token keyword">var</span> canvasWidth<span class="token punctuation">;</span>
<span class="token keyword">var</span> canvasHeight<span class="token punctuation">;</span>

<span class="token keyword">var</span> world<span class="token punctuation">;</span>

<span class="token comment">// 我们将创建世界封装至createWorld函数内</span>
<span class="token keyword">function</span> <span class="token function">createWorld</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 世界的大小</span>
  <span class="token keyword">var</span> worldAABB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  worldAABB<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4000</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  worldAABB<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token number">4000</span><span class="token punctuation">,</span> <span class="token number">4000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">//定义重力</span>
  <span class="token keyword">var</span> gravity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 是否休眠</span>
  <span class="token keyword">var</span> doSleep <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

  <span class="token comment">// 最终创建世界</span>
  <span class="token keyword">var</span> world <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2World</span><span class="token punctuation">(</span>worldAABB<span class="token punctuation">,</span> gravity<span class="token punctuation">,</span> doSleep<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> world<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//绘画功能</span>
<span class="token keyword">function</span> <span class="token function">drawWorld</span><span class="token punctuation">(</span><span class="token parameter">world<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> j <span class="token operator">=</span> world<span class="token punctuation">.</span>m_jointList<span class="token punctuation">;</span> j<span class="token punctuation">;</span> j <span class="token operator">=</span> j<span class="token punctuation">.</span>m_next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 绘制关节</span>
    <span class="token comment">// drawJoint(j, context);</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> b <span class="token operator">=</span> world<span class="token punctuation">.</span>m_bodyList<span class="token punctuation">;</span> b <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> b <span class="token operator">=</span> b<span class="token punctuation">.</span>m_next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> s <span class="token operator">=</span> b<span class="token punctuation">.</span><span class="token function">GetShapeList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> s <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> s <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">GetUserData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 使用数据包括图片</span>
        <span class="token keyword">var</span> img <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetUserData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 图片的长和宽</span>
        <span class="token keyword">var</span> x <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>x<span class="token punctuation">;</span>
        <span class="token keyword">var</span> y <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">GetPosition</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>y<span class="token punctuation">;</span>
        <span class="token keyword">var</span> topleftX <span class="token operator">=</span> <span class="token operator">-</span>img<span class="token punctuation">.</span>clientWidth <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> topleftY <span class="token operator">=</span> <span class="token operator">-</span>img<span class="token punctuation">.</span>clientHeight <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>

        context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">translate</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">rotate</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetRotation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> topleftX<span class="token punctuation">,</span> topleftY<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      <span class="token function">drawShape</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 创建圆形刚体</span>
<span class="token keyword">function</span> <span class="token function">createBall</span><span class="token punctuation">(</span><span class="token parameter">world<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> r<span class="token punctuation">,</span> custom</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 创建圆形定义</span>
  <span class="token keyword">var</span> ballSd <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2CircleDef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  ballSd<span class="token punctuation">.</span>density <span class="token operator">=</span> <span class="token number">1.0</span><span class="token punctuation">;</span> <span class="token comment">// 设置密度</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>custom <span class="token operator">===</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span> ballSd<span class="token punctuation">.</span>density <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
  <span class="token comment">// 若传入'fixed'，则需固定，此时设置密度为0</span>
  <span class="token keyword">else</span> ballSd<span class="token punctuation">.</span>userData <span class="token operator">=</span> custom<span class="token punctuation">;</span> <span class="token comment">// 若传入其他，则视为图片数据</span>
  ballSd<span class="token punctuation">.</span>radius <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span> <span class="token comment">// 设置半径</span>
  ballSd<span class="token punctuation">.</span>restitution <span class="token operator">=</span> <span class="token number">1.0</span><span class="token punctuation">;</span> <span class="token comment">// 设置弹性</span>
  ballSd<span class="token punctuation">.</span>friction <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 设置摩擦因子</span>
  <span class="token keyword">var</span> ballBd <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2BodyDef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建刚体定义</span>
  ballBd<span class="token punctuation">.</span><span class="token function">AddShape</span><span class="token punctuation">(</span>ballSd<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 添加形状</span>
  ballBd<span class="token punctuation">.</span>position<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>x <span class="token operator">||</span> <span class="token number">0</span><span class="token punctuation">,</span> y <span class="token operator">||</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置位置</span>
  <span class="token keyword">return</span> world<span class="token punctuation">.</span><span class="token function">CreateBody</span><span class="token punctuation">(</span>ballBd<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建并返回刚体</span>
<span class="token punctuation">}</span>

<span class="token comment">// 创建矩形刚体</span>
<span class="token keyword">function</span> <span class="token function">createBox</span><span class="token punctuation">(</span><span class="token parameter">world<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">,</span> custom</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> boxSd <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2BoxDef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建一个形状Shape，然后设置有关Shape的属性</span>
  boxSd<span class="token punctuation">.</span>extents<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>width <span class="token operator">||</span> <span class="token number">1200</span><span class="token punctuation">,</span> height <span class="token operator">||</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置矩形高、宽</span>
  boxSd<span class="token punctuation">.</span>density <span class="token operator">=</span> <span class="token number">1.0</span><span class="token punctuation">;</span> <span class="token comment">// 设置矩形的密度</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>custom <span class="token operator">===</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span> boxSd<span class="token punctuation">.</span>density <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
  <span class="token comment">// 若传入'fixed'，则需固定，此时设置密度为0</span>
  <span class="token keyword">else</span> boxSd<span class="token punctuation">.</span>userData <span class="token operator">=</span> custom<span class="token punctuation">;</span> <span class="token comment">// 若传入其他，则视为图片数据</span>
  boxSd<span class="token punctuation">.</span>restitution <span class="token operator">=</span> <span class="token number">0.3</span><span class="token punctuation">;</span> <span class="token comment">// 设置矩形的弹性</span>
  boxSd<span class="token punctuation">.</span>friction <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 设置矩形的摩擦因子，可以设置为0-1之间任意一个数，0表示光滑，1表示强摩擦</span>

  <span class="token keyword">var</span> boxBd <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2BodyDef</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建刚体定义</span>
  boxBd<span class="token punctuation">.</span><span class="token function">AddShape</span><span class="token punctuation">(</span>boxSd<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 添加形状</span>
  boxBd<span class="token punctuation">.</span>position<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>x <span class="token operator">||</span> <span class="token number">10</span><span class="token punctuation">,</span> y <span class="token operator">||</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置位置</span>
  <span class="token keyword">return</span> world<span class="token punctuation">.</span><span class="token function">CreateBody</span><span class="token punctuation">(</span>boxBd<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建并返回刚体</span>
<span class="token punctuation">}</span>

<span class="token comment">// 定义step函数，用于游戏的迭代运行</span>
<span class="token keyword">function</span> <span class="token function">step</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 模拟世界</span>
  world<span class="token punctuation">.</span><span class="token function">Step</span><span class="token punctuation">(</span><span class="token number">1.0</span> <span class="token operator">/</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 清除画布</span>
  ctx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> canvasWidth<span class="token punctuation">,</span> canvasHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 重新绘制</span>
  <span class="token function">drawWorld</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 再次刷新</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span>step<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 从draw_world.js里面引用的绘画功能</span>
<span class="token keyword">function</span> <span class="token function">drawShape</span><span class="token punctuation">(</span><span class="token parameter">shape<span class="token punctuation">,</span> context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#003300&quot;</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>shape<span class="token punctuation">.</span>m_type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 绘制圆</span>
    <span class="token keyword">case</span> b2Shape<span class="token punctuation">.</span>e_circleShape<span class="token operator">:</span>
      <span class="token keyword">var</span> circle <span class="token operator">=</span> shape<span class="token punctuation">;</span>
      <span class="token keyword">var</span> pos <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_position<span class="token punctuation">;</span>
      <span class="token keyword">var</span> r <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_radius<span class="token punctuation">;</span>
      <span class="token keyword">var</span> segments <span class="token operator">=</span> <span class="token number">16.0</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> theta <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> dtheta <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2.0</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">)</span> <span class="token operator">/</span> segments<span class="token punctuation">;</span>
      <span class="token comment">// 画圆圈</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> segments<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>r <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token function">cos</span><span class="token punctuation">(</span>theta<span class="token punctuation">)</span><span class="token punctuation">,</span> r <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token function">sin</span><span class="token punctuation">(</span>theta<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> v <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>pos<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>v<span class="token punctuation">.</span>x<span class="token punctuation">,</span> v<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
        theta <span class="token operator">+=</span> dtheta<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 画半径</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">var</span> ax <span class="token operator">=</span> circle<span class="token punctuation">.</span>m_R<span class="token punctuation">.</span>col1<span class="token punctuation">;</span>
      <span class="token keyword">var</span> pos2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>pos<span class="token punctuation">.</span>x <span class="token operator">+</span> r <span class="token operator">*</span> ax<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos<span class="token punctuation">.</span>y <span class="token operator">+</span> r <span class="token operator">*</span> ax<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>pos2<span class="token punctuation">.</span>x<span class="token punctuation">,</span> pos2<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token comment">// 绘制多边形</span>
    <span class="token keyword">case</span> b2Shape<span class="token punctuation">.</span>e_polyShape<span class="token operator">:</span>
      <span class="token keyword">var</span> poly <span class="token operator">=</span> shape<span class="token punctuation">;</span>
      <span class="token keyword">var</span> tV <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>
        poly<span class="token punctuation">.</span>m_position<span class="token punctuation">,</span>
        b2Math<span class="token punctuation">.</span><span class="token function">b2MulMV</span><span class="token punctuation">(</span>poly<span class="token punctuation">.</span>m_R<span class="token punctuation">,</span> poly<span class="token punctuation">.</span>m_vertices<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
      context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>tV<span class="token punctuation">.</span>x<span class="token punctuation">,</span> tV<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> poly<span class="token punctuation">.</span>m_vertexCount<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> v <span class="token operator">=</span> b2Math<span class="token punctuation">.</span><span class="token function">AddVV</span><span class="token punctuation">(</span>
          poly<span class="token punctuation">.</span>m_position<span class="token punctuation">,</span>
          b2Math<span class="token punctuation">.</span><span class="token function">b2MulMV</span><span class="token punctuation">(</span>poly<span class="token punctuation">.</span>m_R<span class="token punctuation">,</span> poly<span class="token punctuation">.</span>m_vertices<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
        context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>v<span class="token punctuation">.</span>x<span class="token punctuation">,</span> v<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>tV<span class="token punctuation">.</span>x<span class="token punctuation">,</span> tV<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">GetBodyAtPosition</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 首先创建一个近似于点的小区域</span>
  <span class="token keyword">var</span> mousePVec <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 利用b2Vec2定义一个矢量，用来保存鼠标点击的点</span>
  <span class="token keyword">var</span> aabb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 利用b2AABB创建一个环境</span>
  aabb<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  aabb<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span>mousePVec<span class="token punctuation">.</span>x <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">,</span> mousePVec<span class="token punctuation">.</span>y <span class="token operator">+</span> <span class="token number">0.001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 设置aabb的左上角及右下角坐标，这里是以鼠标点击位置为中心创建了一个长、宽均为0.002的矩形区域</span>

  <span class="token comment">// 然后查询与指定区域有重叠的刚体</span>
  <span class="token keyword">var</span> k_maxCount <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token comment">// 设定所要查找形状的数量，注意合理设置其大小，过大会影响运行速度</span>
  <span class="token keyword">var</span> shapes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 保存查找到的与已知边界盒相交的形状</span>
  <span class="token keyword">var</span> count <span class="token operator">=</span> world<span class="token punctuation">.</span><span class="token function">Query</span><span class="token punctuation">(</span>aabb<span class="token punctuation">,</span> shapes<span class="token punctuation">,</span> k_maxCount<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 在世界中查找与边界盒相交的maxCount个形状，并返回边界盒区域内实际包含的形状的个数</span>

  <span class="token keyword">var</span> findBody <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 首先设定没有找到物体</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> count<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">IsStatic</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 条件假定查找到的形状不是静态刚体，比如墙</span>
      <span class="token keyword">var</span> tShape <span class="token operator">=</span> shapes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 将查找到的形状赋给tShape变量</span>
      <span class="token keyword">var</span> inside <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 将tShape对应的刚体赋给inside</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>inside<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 如果inside这个刚体存在</span>
        <span class="token comment">// 那么返回这个刚体，并跳出遍历</span>
        findBody <span class="token operator">=</span> tShape<span class="token punctuation">.</span><span class="token function">GetBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> findBody<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 获取鼠标坐标</span>
<span class="token keyword">function</span> <span class="token function">getMousePos</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> e <span class="token operator">=</span> event <span class="token operator">||</span> window<span class="token punctuation">.</span>event<span class="token punctuation">;</span>
  <span class="token keyword">var</span> scrollX <span class="token operator">=</span> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span>scrollLeft <span class="token operator">||</span> document<span class="token punctuation">.</span>body<span class="token punctuation">.</span>scrollLeft<span class="token punctuation">;</span>
  <span class="token keyword">var</span> scrollY <span class="token operator">=</span> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span>scrollTop <span class="token operator">||</span> document<span class="token punctuation">.</span>body<span class="token punctuation">.</span>scrollTop<span class="token punctuation">;</span>
  <span class="token keyword">var</span> x <span class="token operator">=</span> e<span class="token punctuation">.</span>pageX <span class="token operator">||</span> e<span class="token punctuation">.</span>clientX <span class="token operator">+</span> scrollX<span class="token punctuation">;</span>
  <span class="token keyword">var</span> y <span class="token operator">=</span> e<span class="token punctuation">.</span>pageY <span class="token operator">||</span> e<span class="token punctuation">.</span>clientY <span class="token operator">+</span> scrollY<span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span> x<span class="token operator">:</span> x<span class="token punctuation">,</span> y<span class="token operator">:</span> y <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 处理鼠标移动</span>
<span class="token keyword">function</span> <span class="token function">handleMousedown</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> e <span class="token operator">=</span> e <span class="token operator">||</span> window<span class="token punctuation">.</span>event<span class="token punctuation">;</span>
  <span class="token comment">// 获取鼠标x坐标</span>
  <span class="token keyword">var</span> newMouse <span class="token operator">=</span> <span class="token function">getMousePos</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> selectBody <span class="token operator">=</span> <span class="token function">GetBodyAtPosition</span><span class="token punctuation">(</span>
    newMouse<span class="token punctuation">.</span>x <span class="token operator">-</span> canvas<span class="token punctuation">.</span>offsetLeft<span class="token punctuation">,</span>
    newMouse<span class="token punctuation">.</span>y <span class="token operator">-</span> canvas<span class="token punctuation">.</span>offsetTop
  <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 选择刚体</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>selectBody<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 若有选中刚体，则处理</span>
    <span class="token keyword">var</span> LinearVelocity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 定义一个向量</span>
    selectBody<span class="token punctuation">.</span><span class="token function">WakeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 激活休眠状态</span>
    selectBody<span class="token punctuation">.</span><span class="token function">SetLinearVelocity</span><span class="token punctuation">(</span>LinearVelocity<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//给定一个速度向量</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">// 若无，则随机生成一个矩形添加</span>
    <span class="token keyword">var</span> width <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> height <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> newMouse<span class="token punctuation">.</span>x<span class="token punctuation">,</span> newMouse<span class="token punctuation">.</span>y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

document<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">&quot;mousedown&quot;</span><span class="token punctuation">,</span> handleMousedown<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  canvas <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;canvas&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  ctx <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  canvasWidth <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">)</span><span class="token punctuation">;</span>
  canvasHeight <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 启动游戏</span>
  world <span class="token operator">=</span> <span class="token function">createWorld</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> ball1 <span class="token operator">=</span> <span class="token function">createBall</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> ball2 <span class="token operator">=</span> <span class="token function">createBall</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> box1 <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> box2 <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> box3 <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;box&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> wallLeft <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">,</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> wallRight <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">1290</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> ground <span class="token operator">=</span> <span class="token function">createBox</span><span class="token punctuation">(</span>world<span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">595</span><span class="token punctuation">,</span> <span class="token number">1200</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">&quot;fixed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">step</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><a href="https://github.com/godbasin/box2djs-tutorial/tree/master/6-practice/6-4-mouse-operate-body/6-4-mouse-operate-body" target="_blank" rel="noopener noreferrer">此处查看项目代码（仅包含 src 部分）<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><br> <a href="http://box2d-tutorial.godbasin.com/6-4-mouse-operate-body/index.html" target="_blank" rel="noopener noreferrer">此处查看页面效果<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/box2d-tutorial/6-4-mouse-operate-body.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/box2d-tutorial/6-3-add-bound.html" class="prev">
          6-3. 添加边界
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/box2d-tutorial/6-5-handle-contact.html">
          6-5. 处理碰撞刚体
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-314" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><span class="el-popover__reference-wrapper"><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty0.svg);"></div></span></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.1e2670bf.js" defer></script><script src="/front-end-playground/assets/js/2.38d016d1.js" defer></script><script src="/front-end-playground/assets/js/3.e3f029cb.js" defer></script><script src="/front-end-playground/assets/js/84.5f766c29.js" defer></script>
  </body>
</html>
